package mybatis.hr.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DatabaseManager {
	//SqlSession 객체를 생성해주는 객체. Application에 하나만 생성.
	private SqlSessionFactory factory;
	
	private static DatabaseManager instance;
	
	private DatabaseManager(){
		//SqlSessionFactory는 SqlSessionFactoryBuiler를 통해 생성 - mybatis 설정 전달
		// - mybatis 설정 전달(InputStream)
		InputStream in = null;
		try {
			in = Resources.getResourceAsStream("mybatis/hr/dao/config/mybatis-config.xml"); 
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		factory = builder.build(in);
	}
	
	public static DatabaseManager getInstance(){
		if(instance==null){
			instance = new DatabaseManager();
		}
		return instance;
	}
	
	public SqlSessionFactory getSqlSessionFactory(){
		return factory;
	}
}
